<template>
  <div class="flexContent">
    <!-- 激活码管理 -->
    <div class="searchBox">
      <el-form ref="formRef" class="demo-form-inline" @submit.native.prevent label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="激活码">
                  <el-input v-model="code" style="width:95%" placeholder="请输入..." clearable
                    @keyup.enter.native="serchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="设备编号">
                  <el-input v-model="deviceNum" placeholder="请输入..." style="width: 95%" clearable
                    @keyup.enter.native="serchFn" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="serchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" @click="addFn">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" class="tablebox" border ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          align="center">
          <vxe-column type="seq" title="序号" width="80"></vxe-column>
          <vxe-column field="code" title="激活码" />
          <vxe-column field="deviceNum" title="设备编号" />
          <vxe-column field="deviceId" title="设备ID" />
          <vxe-column field="cashierId" title="设备状态">
            <template v-slot="{ row }">
              <el-tag v-if="row.status === '0'" disabled type="primary" plain size="small">
                已启用
              </el-tag>
              <el-tag v-if="row.status === '1'" disabled type="info" plain size="small">
                已停用
              </el-tag>
              <el-tag v-if="row.status === '2'" disabled type="danger" plain size="small">
                故 障
              </el-tag>
            </template>
          </vxe-column>
          <vxe-column field="updateTime" title="设备状态">
            <template v-slot="{ row }">
              {{ row.updateTime | dateFormat }}
            </template>
          </vxe-column>
          <vxe-column title="操作" width="200">
            <template v-slot="{ row }">
              <el-button type="text" size="big" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click.stop="getDetail(row)">
                详情
              </el-button>
              <el-button slot="reference" style="color: rgb(0, 132, 255)" type="text" size="big" @click.stop="edit(row)">
                修改
              </el-button>
              <el-button slot="reference" style="color: red" type="text" size="big" @click.stop="$delFn('激活码', del, row)">
                删除
              </el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
    <AddDialog :close-on-click-modal="false" :show-dialog.sync="showDialog" :width="width" :title="title" @btnOk="btnOk"
      @close="close">
      <el-form ref="addFormRef" :disabled="title === '详情'" :model="addForm" :rules="addRules" label-width="130px">
        <el-form-item label="激活码" prop="code">
          <el-input v-model="addForm.code" :disabled="title !== '新增'" />
        </el-form-item>
        <el-form-item label="绑定设备" prop="cashierId">
          <el-select v-model="addForm.cashierId" filterable clearable popper-class="eloption">
            <el-option v-for="pos in posList" :key="pos.id" :value="pos.id" :label="pos.deviceNum" />
          </el-select>
        </el-form-item>
      </el-form>
    </AddDialog>
  </div>
</template>

<script>
import { parseTime6 } from '@/utils/index'
import AddDialog from '@/components/Goods/AddDialog';
import {
  getShopAiCode,
  getShopAiCodeDetail,
  addShopAiCode,
  bindShopAiCode,
  delShopAiCode,
  getPosBindCodeList
} from '@/api/shop';
import closeTag from '@/utils/closeTag';
export default {
  name: 'CodeInfo',
  components: {
    AddDialog
  },
  filters: {
    dateFormat(val) {
      if (val) {
        return parseTime6(val);
      }
    }
  },
  data() {
    return {
      width: '35%',
      title: '新增',
      showDialog: false,
      addForm: {
        cashierId: '', // 绑定设备
        code: '', // 激活码
      },
      posList: [],
      codeList: [],
      addRules: {
        code: [
          {
            required: true,
            message: '请输入激活码',
            trigger: 'blur'
          }
        ],
      },
      isSearch: false,
      deviceNum: '', // 设备编号
      code: '',
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      codeId: ''
    };
  },
  created() {
    this.getCodeList();
    this.getPosBindCodeList();
  },
  methods: {
    async getPosBindCodeList() {
      const res = await getPosBindCodeList()
      if (res.code === 200) {
        const result = res.data;
        this.posList = result;
        console.log('posList', this.posList);
      } else {
        // this.$message.error(res.message);
      }
    },
    async getCodeList() {
      if (!this.isSearch) {
        this.deviceNum = '';
        this.code = '';
      }
      const res = await getShopAiCode({
        ...this.page,
        deviceNum: this.deviceNum,
        code: this.code,
      });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.codeList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log('codeList', this.codeList);
        const $table = this.$refs.xTable1
        $table.loadData(this.codeList);
      } else {
        // this.$message.error(res.message);
      }
    },
    async serchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0
      };
      this.isSearch = true;
      this.getCodeList();
    },
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0
      };
      this.deviceNum = '';
      this.code = '';
      this.isSearch = false;
      this.getCodeList();
    },
    addFn() {
      this.getPosBindCodeList();
      this.title = '新增';
      this.showDialog = true;
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getCodeList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getCodeList();
    },
    close() {
      this.showDialog = false;
      this.addForm = {
        cashierId: '',
        code: ''
      };
      this.$refs.addFormRef.resetFields();
    },
    btnOk() {
      // 判断是新增还是修改
      if (this.title === '新增') {
        // 新增
        this.$refs.addFormRef.validate(async isOk => {
          if (isOk) {
            //
            console.log('this.addForm', this.addForm);
            // return
            const res = await addShopAiCode(this.addForm);
            if (res.code === 200) {
              this.$message.success('新增成功');
              this.getCodeList();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning('请检查输入信息');
          }
        });
      } else if (this.title === '详情') {
        // 关闭
        this.close();
      } else if (this.title === '修改') {
        // 修改
        this.$refs.addFormRef.validate(async isOk => {
          if (isOk) {
            console.log('this.addForm', this.addForm);
            const res = await bindShopAiCode({
              ...this.addForm,
              id: this.codeId
            });
            if (res.code === 200) {
              this.$message.success('修改成功');
              this.getCodeList();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning('请检查输入信息');
          }
        });
      }
    },
    async del(row) {
      // 删除
      const res = await delShopAiCode({
        id: row.id
      });
      if (res.code === 200) {
        this.$message.success('删除成功');
        this.getCodeList();
      } else {
        // this.$message.error(res.message);
      }
    },
    async getDetail(row) {
      this.showDialog = true;
      this.title = '详情';
      console.log(row);
      this.getPosBindCodeList();
      // const res = await getShopAiCodeDetail({ id: row.id });
      // if (res.code === 200) {
      //   const data = res.data;
      this.addForm.cashierId = row.cashierId;
      this.addForm.code = row.code;
      this.posList.push({
        id: row.cashierId,
        deviceNum: row.deviceNum
      })
      // } else {
      // this.$message.error(res.message);
      // }
    },
    async edit(row) {
      this.showDialog = true;
      this.title = '修改';
      console.log(row);
      this.codeId = row.id;
      this.getPosBindCodeList();
      // const res = await getShopAiCodeDetail({ id: row.id });
      // if (res.code === 200) {
      //   const data = res.data;
      //   console.log(data, 'data');
      this.addForm.cashierId = row.cashierId;
      this.addForm.code = row.code;
      this.posList.push({
        id: row.cashierId,
        deviceNum: row.deviceNum
      })
      console.log('row222', row, this.posList);
      // } else {
      // this.$message.error(res.message);
      // }
    }
  }
};
</script>

<style lang="scss" scoped>
.shopInfoContent {
  padding: 10px;

  /* // background-color: #eceaea; */
  .searchBox {
    padding-top: 20px;
    /* // height: 20%; */
    background-color: #fff;
    margin-bottom: 10px;
  }

  .table {
    padding: 20px;
    background: #fff;
  }
}
</style>
